﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Security_Group_Detail : DetailPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initializeWebForm();

        if (!IsPostBack)
        {
            Guid _roleGuid = Guid.Empty;

            if (Request.QueryString["RoleGuid"] != null)
            {
                _roleGuid = new Guid(Request.QueryString["RoleGuid"]);
            }

            if (_roleGuid != Guid.Empty)
            {
                Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

                System.Data.DataTable roleTable = securityProvider.RoleGetByRoleGuid(_roleGuid);

                if (roleTable.Rows.Count == 1)
                {
                    System.Data.DataRow roleRow = roleTable.Rows[0];

                    roleIdTextBox.Text = System.Convert.ToString(roleRow["RoleId"]);
                    roleGuidTextBox.Text = System.Convert.ToString(roleRow["RoleGuid"]);

                    groupNameTextBox.Text = System.Convert.ToString(roleRow["RoleName"]);
                    groupDescriptionTextBox.Text = System.Convert.ToString(roleRow["RoleDescription"]);
                }
            }

            switch (formMode)
            {
                case FormMode.Create:
                    groupNameTextBox.Enabled = true;
                    groupDescriptionTextBox.Enabled = true;

                    groupNameTextBox.ReadOnly = false;
                    groupDescriptionTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Delete:
                    groupNameTextBox.Enabled = true;
                    groupDescriptionTextBox.Enabled = true;

                    groupNameTextBox.ReadOnly = true;
                    groupDescriptionTextBox.ReadOnly = true;

                    deleteImageButton.Visible = true;
                    break;
                case FormMode.Edit:
                    groupNameTextBox.Enabled = true;
                    groupDescriptionTextBox.Enabled = true;

                    groupNameTextBox.ReadOnly = false;
                    groupDescriptionTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Default:
                    groupNameTextBox.Enabled = true;
                    groupDescriptionTextBox.Enabled = true;

                    groupNameTextBox.ReadOnly = true;
                    groupDescriptionTextBox.ReadOnly = true;

                    deleteImageButton.Visible = false;
                    break;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(this.buildForwardUrl("Default.aspx"));
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

        if (formMode == FormMode.Edit)
        {
            if (!securityProvider.RoleUpdate(
                new Guid(roleGuidTextBox.Text),
                System.Convert.ToInt32(roleIdTextBox.Text),
                groupNameTextBox.Text,
                groupDescriptionTextBox.Text))
            {
                Response.Write("alert(\"An error occured while updating the database\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
        else if (formMode == FormMode.Create)
        {
            if (securityProvider.RoleInsert(
                Guid.NewGuid(),
                groupNameTextBox.Text,
                groupDescriptionTextBox.Text) == 0)
            {
                Response.Write("alert(\"An error occured while creating database entry\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
    }
}
